package com.tanhua.admin.service;

import com.tanhua.commons.utils.Constants;
import com.tanhua.dubbo.api.FreezeApi;
import com.tanhua.dubbo.api.MovementApi;
import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.dubbo.api.VideoApi;
import com.tanhua.model.domain.FreezeDetail;
import com.tanhua.model.domain.UserInfo;
import com.tanhua.model.dto.BaseUserInfoDto;
import com.tanhua.model.dto.MovementDto;
import com.tanhua.model.enums.FreezingRange;
import com.tanhua.model.enums.FreezingTime;
import com.tanhua.model.mongo.Movement;
import com.tanhua.model.mongo.Video;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.VideoVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.time.Duration;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class ManageService {

    @DubboReference
    private UserInfoApi userInfoApi;

    @DubboReference
    private FreezeApi freezeApi;

    @DubboReference
    private VideoApi videoApi;

    @DubboReference
    private MovementApi movementApi;

    @Autowired
    private RedisTemplate redisTemplate;

    public void unfreeze(Integer userId) {

        FreezeDetail freezeDetail = freezeApi.unfreeze(userId);
        if (freezeDetail.getFreezingRange() == FreezingRange.LOGIN.getType()) {
            redisTemplate.delete(Constants.FreezingRange_LOGIN+ userId);
        } else if (freezeDetail.getFreezingRange() == FreezingRange.SAY.getType()) {
            redisTemplate.delete(Constants.FreezingRange_SAY+ userId);
        } else {
            redisTemplate.delete(Constants.FreezingRange_PUBLISH+ userId);
        }
        UserInfo userInfo = new UserInfo();
        userInfo.setId(Long.valueOf(userId));
        userInfo.setUserStatus("1");
        userInfoApi.update(userInfo);

    }

    public PageResult select(Long id, String nickname, String city, Integer pagesize, Integer page) {

        return userInfoApi.select(id,nickname,city,page,pagesize);

    }

    public BaseUserInfoDto selectOne(Long id) {
        return userInfoApi.selectOne(id);
    }

    public void freeze(FreezeDetail freezeDetail) {
        if (freezeDetail.getFreezingTime() == FreezingTime.THREE.getType()) {
            Date date = new Date();
            long time = date.getTime() + 3600 * 1000 * 24 * 3;
            if (freezeDetail.getFreezingRange() == FreezingRange.LOGIN.getType()) {
                redisTemplate.opsForValue().set(Constants.FreezingRange_LOGIN+ freezeDetail.getUserId(), time, Duration.ofDays(3));
            } else if (freezeDetail.getFreezingRange() == FreezingRange.SAY.getType()) {
                redisTemplate.opsForValue().set(Constants.FreezingRange_SAY + freezeDetail.getUserId(), time, Duration.ofDays(3));
            } else {
                redisTemplate.opsForValue().set(Constants.FreezingRange_PUBLISH + freezeDetail.getUserId(), time, Duration.ofDays(3));
            }
            freezeDetail.setUnfreezeTime(time);
        } else if (freezeDetail.getFreezingTime() == FreezingTime.SEVEN.getType()) {
            Date date = new Date();
            long time = date.getTime() + 3600 * 1000 * 24 * 7;
            if (freezeDetail.getFreezingRange() == FreezingRange.LOGIN.getType()) {
                redisTemplate.opsForValue().set(Constants.FreezingRange_LOGIN + freezeDetail.getUserId(), time, Duration.ofDays(7));
            } else if (freezeDetail.getFreezingRange() == FreezingRange.SAY.getType()) {
                redisTemplate.opsForValue().set(Constants.FreezingRange_SAY + freezeDetail.getUserId(), time, Duration.ofDays(7));
            } else {
                redisTemplate.opsForValue().set(Constants.FreezingRange_PUBLISH + freezeDetail.getUserId(), time, Duration.ofDays(7));
            }
            freezeDetail.setUnfreezeTime(time);
        } else {
            if (freezeDetail.getFreezingRange() == FreezingRange.LOGIN.getType()) {
                redisTemplate.opsForValue().set(Constants.FreezingRange_LOGIN + freezeDetail.getUserId(), "never");
            } else if (freezeDetail.getFreezingRange() == FreezingRange.SAY.getType()) {
                redisTemplate.opsForValue().set(Constants.FreezingRange_SAY + freezeDetail.getUserId(), "never");
            } else {
                redisTemplate.opsForValue().set(Constants.FreezingRange_PUBLISH + freezeDetail.getUserId(), "never");
            }
            freezeDetail.setUnfreezeTime(0l);
        }
      /*  if (freezeDetail.getFreezingTime() == FreezingTime.THREE.getType()) {
            Date date = new Date();
            long time = date.getTime() + 3600 * 1000 * 24 * 3;
            freezeDetail.setUnfreezeTime(time);
        } else if (freezeDetail.getFreezingTime() == FreezingTime.SEVEN.getType()) {
            Date date = new Date();
            long time = date.getTime() + 3600 * 1000 * 24 * 7;
            freezeDetail.setUnfreezeTime(time);
        } else {
            freezeDetail.setUnfreezeTime(0l);
        }*/
        freezeApi.freeze(freezeDetail);
        UserInfo userInfo = new UserInfo();
        userInfo.setId(Long.valueOf(freezeDetail.getUserId()));
        userInfo.setUserStatus("2");
        userInfoApi.update(userInfo);

    }


    public PageResult selectVideos(Long uid, Integer page, Integer pagesize, String sortOrder, String sortProp) {

        List<Video> list = videoApi.selectVideos(uid, page, pagesize, sortOrder, sortProp);

        List<VideoVo> vos = list.stream().map(item -> {
            UserInfo userInfo = userInfoApi.findById(item.getUserId());
            VideoVo vo = VideoVo.init(userInfo, item);
            return vo;
        }).collect(Collectors.toList());
        return new PageResult(page, pagesize, 0l, vos);
    }

    //
    public PageResult selectMessages(Integer page, Integer pagesize, String sortOrder, String sortProp, Long uid, Integer state) {
        List<Movement> movements = movementApi.findByUserId(uid, page, pagesize, sortOrder, sortProp, state);
        List<MovementDto> dtos = movements.stream().map(item -> {
            UserInfo userInfo = userInfoApi.findById(item.getUserId());
            MovementDto dto = new MovementDto();
            BeanUtils.copyProperties(item, dto);
            dto.setCreateDate(item.getCreated());
            dto.setText(item.getTextContent());
            dto.setNickname(userInfo.getNickname());
            dto.setUserLogo(userInfo.getAvatar());
            return dto;
        }).collect(Collectors.toList());
        return new PageResult(page, pagesize, 0l, dtos);
    }
}

